import { Component, OnInit } from '@angular/core';
import { FetchService } from "../../../../core/net/fetch.service";
import * as _ from 'lodash';
import { format, subDays } from 'date-fns';

import { DomSanitizer, SafeResourceUrl } from "@angular/platform-browser";
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-report-ysl',
  templateUrl: './ysl.component.html',
  styleUrls: ['../report.component.scss', './ysl.component.scss']
})
export class YslComponent implements OnInit {
  yearValue = 2020;
  monthPickerValue = new Date().getMonth();
  areaType = 1;
  serverUrl = (window as any).environment.reportUrl;
  iframeSrc: SafeResourceUrl;
  range = ["", new Date()];

  selectedValue = "10";
  orderTypeText = "正序";
  orderType = true;
  frId = "10";
  sort = "1";
  irrigationType = 1;




  constructor(
    private fetchService: FetchService,
    private sanitizer: DomSanitizer,
    private message: NzMessageService

  ) {
  }

  ngOnInit() {
    if (parseFloat( format(this.range[1], 'MM')) > 9) {
      this. range = ["2021-10-1", new Date()];
     } else {
       this. range = ["2021-1-1", new Date()];
     }
    this.queryInfo();
  }

  queryInfo() {

    const beginTime = format(this.range[0], 'YYYY-MM-DD');
    const endTime = format(this.range[1], 'YYYY-MM-DD');
    console.log(format(this.range[1], 'YYYY'));
    const year = this.yearValue;
    const month = this.monthPickerValue + 1;
    const reportName = '';
    // this.irrigationType === 1 ? reportName = "灌溉进度报表1" : reportName = "灌溉进度报表2";
    const token = localStorage.token;
    let link = "";
    if (this.areaType === 1) {
      link = `${this.serverUrl}preview?_u=wrs:灌溉用水量市县.ureport.xml&_t=6&edDate=${endTime}&orderBy=${this.sort}&stDate=${beginTime}`;
    } else {
      console.log(this.frId);
      if (this.frId === '19') {
        link = `${this.serverUrl}preview?_u=wrs:灌溉用水量.ureport.xml&_t=6&edDate=${endTime}&fId=19&orderBy=${this.sort}&stDate=${beginTime}&classify=GHYS`;

      } else if (this.frId === '22') {
        link = `${this.serverUrl}preview?_u=wrs:灌溉用水量.ureport.xml&_t=6&edDate=${endTime}&fId=19&orderBy=${this.sort}&stDate=${beginTime}&classify=GHKG`;

      } else {
        link = `${this.serverUrl}preview?_u=wrs:灌溉用水量.ureport.xml&_t=6&edDate=${endTime}&fId=${this.frId}&orderBy=${this.sort}&stDate=${beginTime}`;

      }

    }

    this.iframeSrc = this.sanitizer.bypassSecurityTrustResourceUrl(link);
  }

  dateChangeActive(val) {
    // const stDate = format(this.range[0], 'YYYY-MM').split("-")[0];
    // const edDate = format(this.range[1], 'YYYY-MM').split("-")[0];
    // const stDateActive = format(this.range[0], 'YYYY-MM').split("-")[1];
    // const edDateActive = format(this.range[1], 'YYYY-MM').split("-")[1];
    this.queryInfo();
    // if (stDate != edDate) {
    //   this.message.error('灌季不能跨年选择!');
    //   return false;
    // } else {
    //   if (Number(stDateActive) <= 9 && Number(edDateActive) > 9) {
    //     this.message.error('不能跨灌季选择!');
    //     return false;

    //   } else {
    //     this.queryInfo();
    //   }
    // }



  }
  // 选择管理处
  onchangeGlc(val) {
    this.frId = val;
    this.queryInfo();

  }
  dateChange(val) {
    this.areaType = val;
    this.queryInfo();
  }
  // 改变升降序
  changeOrderType() {
    this.orderType = !this.orderType;
    this.orderType ? this.orderTypeText = "正序" : this.orderTypeText = "倒序";
    this.orderType ? this.sort = "1" : this.sort = "2";
    this.queryInfo();
  }
  irrigationChange(val) {
    this.irrigationType = val;

    this.queryInfo();
  }
}

